﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.hangqing
{
    public partial class hangqing_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.hangqing().Exists(this.id))
                {
                    JscriptMsg("内容不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                GetCaizhi();
                GetProvince();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                    txtClick.Text = Utils.Number(3);
                }
                GetSizeList();
            }
        }
        #region 绑定类别=================================
        private void GetCaizhi()
        {
            BLL.caizhi bll = new BLL.caizhi();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id desc").Tables[0];

            this.ddlCaizhiID.Items.Clear();
            this.ddlCaizhiID.Items.Add(new ListItem("请选择材质", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();

                this.ddlCaizhiID.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定省份=================================
        private void GetProvince()
        {
            BLL.province bll = new BLL.province();
            DataTable dt = bll.GetList(0, "", "id asc").Tables[0];

            this.ddlProvince.Items.Clear();
            this.ddlProvince.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["province_name"].ToString().Trim();

                this.ddlProvince.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetCity(int province_id)
        {
            BLL.city bll = new BLL.city();
            DataTable dt = bll.GetList(0, "province_id=" + province_id, "id asc").Tables[0];

            this.ddlCity.Items.Clear();
            this.ddlCity.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["city_name"].ToString().Trim();

                this.ddlCity.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetDistrict(int city_id)
        {
            BLL.district bll = new BLL.district();
            DataTable dt = bll.GetList(0, "city_id=" + city_id, "id asc").Tables[0];

            this.ddlDistrict.Items.Clear();
            this.ddlDistrict.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["district_name"].ToString().Trim();

                this.ddlDistrict.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.hangqing bll = new BLL.hangqing();
            Model.hangqing model = bll.GetModel(_id);

            rblStatus.SelectedValue = model.status.ToString();

            ddlProvince.SelectedValue = model.province_id.ToString();
            if (model.province_id > 0)
            {
                GetCity(model.province_id);
            }
            ddlCity.SelectedValue = model.city_id.ToString();
            if (model.city_id > 0)
            {
                GetDistrict(model.city_id);
            }
            ddlDistrict.SelectedValue = model.district_id.ToString();

            txtTitle.Text = model.title;
            txtImgUrl.Text = model.img_url;
            ddlCaizhiID.SelectedValue = model.caizhi_id.ToString();
            txtClick.Text = model.hits.ToString();
            txtSortId.Text = model.sort_id.ToString();

            //绑定内容附件
            rptHongqingPrice.DataSource = model.price;
            rptHongqingPrice.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            try
            {
                Model.hangqing model = new Model.hangqing();
                BLL.hangqing bll = new BLL.hangqing();

                model.title = Request.Form["txtTitle"].Trim();
                model.img_url = txtImgUrl.Text.Trim();
                model.caizhi_id = Utils.StrToInt(ddlCaizhiID.SelectedValue, 0);

                model.province_id = Utils.StrToInt(ddlProvince.SelectedValue, 0);
                model.city_id = Utils.StrToInt(ddlCity.SelectedValue, 0);
                model.district_id = Utils.StrToInt(ddlDistrict.SelectedValue, 0);

                model.hits = int.Parse(txtClick.Text.Trim());
                model.sort_id = int.Parse(txtSortId.Text.Trim());
                model.update_time = DateTime.Now;
                model.status = int.Parse(rblStatus.SelectedValue);

                #region 保存套餐====================
                //保存附件
                string[] titleArr = Request.Form.GetValues("hide_title");
                string[] priceArr = Request.Form.GetValues("txt_price");
                string[] isupArr = Request.Form.GetValues("hide_is_up");
                string[] sortIdArr = Request.Form.GetValues("txt_sortid");

                if (priceArr != null && sortIdArr != null && isupArr!=null && priceArr.Length > 0 && sortIdArr.Length > 0 && isupArr.Length>0)
                {
                    List<Model.hangqing_price> ls = new List<Model.hangqing_price>();
                    for (int i = 0; i < priceArr.Length; i++)
                    {
                        ls.Add(new Model.hangqing_price 
                        {
                            title = titleArr[i],
                            price = priceArr[i],
                            is_up = Utils.StrToInt(isupArr[i], 0),
                            sort_id = Utils.StrToInt(sortIdArr[i], 99)
                        });
                    }
                    model.price = ls;
                }
                #endregion

                if (bll.Add(model) > 0)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            try
            {
                BLL.hangqing bll = new BLL.hangqing();
                Model.hangqing model = bll.GetModel(_id);

                model.title = Request.Form["txtTitle"].Trim();
                model.img_url = txtImgUrl.Text.Trim();
                model.caizhi_id = Utils.StrToInt(ddlCaizhiID.SelectedValue, 0);
                model.province_id = Utils.StrToInt(ddlProvince.SelectedValue, 0);
                model.city_id = Utils.StrToInt(ddlCity.SelectedValue, 0);
                model.district_id = Utils.StrToInt(ddlDistrict.SelectedValue, 0);

                model.hits = int.Parse(txtClick.Text.Trim());
                model.sort_id = int.Parse(txtSortId.Text.Trim());
                model.update_time = DateTime.Now;
                model.status = int.Parse(rblStatus.SelectedValue);

                #region 保存价格====================
                if (model.price != null)
                {
                    model.price.Clear();
                }
                string[] priceIdArr = Request.Form.GetValues("hide_price_id");
                string[] titleArr = Request.Form.GetValues("hide_title");
                string[] priceArr = Request.Form.GetValues("txt_price");
                string[] isupArr = Request.Form.GetValues("hide_is_up");
                string[] sortIdArr = Request.Form.GetValues("txt_sortid");

                if (priceIdArr != null && titleArr != null && priceArr != null && sortIdArr != null && isupArr != null && priceIdArr.Length > 0
                   && titleArr.Length > 0 && priceArr.Length > 0 && sortIdArr.Length > 0 && isupArr.Length > 0)
                {
                    List<Model.hangqing_price> ls = new List<Model.hangqing_price>();
                    for (int i = 0; i < titleArr.Length; i++)
                    {
                        ls.Add(new Model.hangqing_price
                        {
                            id = Utils.StrToInt(priceIdArr[i], 0),
                            title = titleArr[i],
                            price = priceArr[i],
                            is_up = Utils.StrToInt(isupArr[i], 0),
                            sort_id = Utils.StrToInt(sortIdArr[i], 99),
                        });
                    }
                    model.price = ls;
                }
                #endregion

                if (bll.Update(model))
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        //保存类别
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改行情信息成功！", "hangqing_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加行情信息成功！", "hangqing_list.aspx");
            }
        }

        protected void GetSizeList()
        {
            BLL.hangqing_size bll = new BLL.hangqing_size();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id desc").Tables[0];
            this.rptSizeList.DataSource = dt;
            this.rptSizeList.DataBind();
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            int province_id = int.Parse(ddlProvince.SelectedValue);
            GetCity(province_id);
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            int city_id = int.Parse(ddlCity.SelectedValue);
            GetDistrict(city_id);
        }
    }
}